home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMIBEST4.ADF / AmigaBasicStuff / Meadows3D.MSB (.txt) < prev    next >
AmigaBASIC Source Code  |  1987-07-22  |  8KB  |  406 lines

  1.  
  2. ' Try3d -- An Example of 3D Programming 
  3. ' Initial Amiga implementation 
  4. '  by Jim Meadows 6/1/86 Compuserve [75046,2012]
  5.  
  6. ' 3D Greeting
  7. CLS:PRINT
  8. COLOR 3:PRINT "    Try3D";
  9. COLOR 1:PRINT " -- An Example of 3D Programming"
  10. COLOR 1:PRINT "             by Jim Meadows"
  11. GOSUB InitVals
  12. GOSUB SetImage
  13. ' GOSUB SetImage
  14. ' GOTO Manual
  15. ef=-1
  16. ax=10:ay=5:px=180:py=70
  17. GOSUB DrawImage
  18. LOCATE 18,20:COLOR 3:PRINT "Hello!"
  19. GOSUB Pause
  20. ax=10:ay=-30
  21. GOSUB DrawImage
  22. LOCATE 18,3:COLOR 3:PRINT "Welcome to the World of 3D Graphics!"
  23. GOSUB Pause
  24. ax=30:ay=20
  25. GOSUB DrawImage
  26. LOCATE 18,15:COLOR 3:PRINT "Hmmmmmm....."
  27. GOSUB Pause
  28. ax=-30:ay=-15:az=20:di=500
  29. GOSUB DrawImage
  30. LOCATE 18,8:COLOR 3:PRINT "Yep, just as I figured ..."
  31. GOSUB Pause
  32. az=-20:ay=15
  33. GOSUB DrawImage
  34. LOCATE 18,4:COLOR 3:PRINT "You've got what it takes for 3D!"
  35. GOSUB Pause
  36. ax=10:ay=-20:az=0:di=900
  37. GOSUB DrawImage
  38. LOCATE 18,15:COLOR 3:PRINT "You have ..."
  39. GOSUB Pause
  40. ax=-5:ay=0:di=700
  41. GOSUB DrawImage
  42. LOCATE 18,15:COLOR 3:PRINT "... an AMIGA!!"
  43. FOR i = 1 TO 10000:NEXT
  44. ef=0
  45.  
  46. Ri:
  47. '  Rotating Image
  48. CLS
  49. LOCATE 19,13:COLOR 3:PRINT "Delta Wing Fighter"
  50. GOSUB SetImage
  51. ' Draw and Undraw rotating image
  52. ax=-90:ay=270:az=0:px=160:py=100
  53. FOR ii = 1 TO 4
  54.   GOSUB DrawImage
  55.   GOSUB Pause
  56.   ef=1
  57.   GOSUB DrawImage
  58.   ef=0
  59.   ax=-20
  60.   ay=ay-60
  61.   IF ii=1 THEN ax=0:ay=270:az=0
  62. NEXT
  63.   
  64. Fly:
  65. ' Animated flight path 
  66. FOR inum = 1 TO 2
  67.   IF inum=2 THEN GOSUB SetImage
  68.   di=2400
  69.   ax=0:ay=270:az=0
  70.   px=30:py=30:ef=-1
  71.   GOSUB DrawImage
  72.   px=px+40:ax=ax-5:di=di-100
  73.   GOSUB DrawImage
  74.   ax=ax-5:di=di-100
  75.   FOR R=1 TO 3
  76.     px=px+40:ay=ay-20:di=di-100
  77.     GOSUB DrawImage
  78.   NEXT
  79.   FOR R = 1 TO 3
  80.     px=px+30:ay=ay-20:az=az-20:di=di-100
  81.     GOSUB DrawImage
  82.   NEXT
  83.   FOR R=1 TO 4
  84.     px=px-20:py=py+10:az=az+10:di=di-80
  85.     GOSUB DrawImage
  86.   NEXT  
  87.   FOR R=1 TO 8
  88.     px=px-9*R:py=py+10:az=az+5:di=di-60:ax=ax-5
  89.     GOSUB DrawImage
  90.   NEXT
  91. NEXT
  92.  
  93.  
  94. ' Finally allow manual control
  95. GOSUB SetImage
  96.  
  97. Manual:
  98.  
  99. MENU 1,0,1,"Rotate +"
  100. MENU 1,1,1,"Around X-axis"
  101. MENU 1,2,1,"Around Y-axis"
  102. MENU 1,3,1,"Around Z-axis"
  103.  
  104. MENU 2,0,1,"Rotate -"
  105. MENU 2,1,1,"Around X-axis"
  106. MENU 2,2,1,"Around Y-axis"
  107. MENU 2,3,1,"Around Z-axis"
  108.  
  109. MENU 3,0,1,"Move"
  110. MENU 3,1,1,"Closer"
  111. MENU 3,2,1,"Away"
  112. MENU 3,3,1,"Right"
  113. MENU 3,4,1,"Left"
  114. MENU 3,5,1,"Up"
  115. MENU 3,6,1,"Down"
  116.  
  117. MENU 4,0,1,"Reset"
  118. MENU 4,1,1,"Angles"
  119. MENU 4,2,1,"Distance"
  120. MENU 4,3,1,"Position"
  121. MENU 4,4,1,"Quit"
  122.  
  123. ON MENU GOSUB Menus
  124. ON MOUSE GOSUB Mous
  125.  
  126. m1=1:GOSUB Reeset
  127. m1=2:GOSUB Reeset
  128. m1=3:GOSUB Reeset
  129. act=1:ef=-1
  130.  
  131. MOUSE ON
  132. MENU ON
  133.  
  134. Loop:
  135.   IF act=0 THEN inc=1: GOTO Loop
  136.   GOSUB DrawImage
  137.   GOSUB Vals
  138.   IF MOUSE(0)<>-1 THEN act=0 :ELSE GOSUB Mous
  139.   GOTO Loop
  140.  
  141.                
  142. '-------------
  143. ' Subroutines
  144. '-------------
  145.  
  146. Vals:
  147.   COLOR 1
  148.   LOCATE 1,1:PRINT "Ax,Ay,Az: "ax","ay","az
  149.   LOCATE 2,1:PRINT "Px,Py   : "px","py
  150.   LOCATE 3,1:PRINT "Di      : "di
  151.   COLOR 3
  152.   LOCATE 4,1:PRINT "Use Menus to Change View"
  153.   COLOR 2
  154.   LOCATE 5,1:PRINT "(press left button to repeat)"
  155. RETURN
  156.  
  157. Menus:
  158.  act=1
  159.  inc=1
  160.  m0=MENU(0)
  161.  m1=MENU(1)
  162.  ON m0 GOSUB RotateP,RotateM,MoveI,Reeset
  163. RETURN
  164.  
  165. Mous:  
  166.   act=1
  167.   inc=inc+0.5
  168.   ON m0 GOSUB RotateP,RotateM,MoveI,Reeset
  169.   RETURN  
  170.  
  171. RotateP:
  172.   IF m1=1 THEN ax=ax+10*inc
  173.   IF m1=2 THEN ay=ay+10*inc
  174.   IF m1=3 THEN az=az+10*inc
  175. RETURN
  176.  
  177. RotateM:
  178.   IF m1=1 THEN ax=ax-10*inc
  179.   IF m1=2 THEN ay=ay-10*inc
  180.   IF m1=3 THEN az=az-10*inc
  181. RETURN
  182.  
  183. MoveI:
  184.   IF m1=1 THEN di=di-50*inc
  185.   IF m1=2 THEN di=di+50*inc
  186.   IF m1=3 THEN px=px+20*inc
  187.   IF m1=4 THEN px=px-20*inc
  188.   IF m1=5 THEN py=py-10*inc
  189.   IF m1=6 THEN py=py+10*inc
  190. RETURN
  191.      
  192. Reeset:
  193.   IF m1=1 THEN ax=-15:ay=-25:az=0
  194.   IF m1=2 THEN di=1200 
  195.   IF m1=3 THEN px=160:py=100
  196.   IF m1=4 THEN  MENU OFF:END
  197. RETURN       
  198.  
  199. Pause:
  200.   FOR i = 1 TO 4000:NEXT
  201. RETURN
  202.   
  203.   
  204.   
  205. ' ----------------------------------
  206. ' |          3-D Routines          |
  207. ' ----------------------------------
  208.  
  209. ' ax,ay,az = rotation Angle in degrees
  210. ' di = distance to image
  211. ' dw = distance to window (projection plane)
  212. ' px,py = position of image on screen
  213. ' sf = screen scaling factor
  214. ' ef = erase flag (1=erase, 0=draw, -1=cls & draw)
  215. ' Image data is at end of program
  216.  
  217. InitVals:
  218.   ' Define Arrays
  219.   DIM it%(100,3):' Image Table
  220.   DIM rim%(100,3):' Rotated Image
  221.   ' Initialize Values
  222.   x=0:y=0:z=0
  223.   dw=400:' Distance to window
  224.   di=900:' Distance to image
  225.   sf=2.35:' Screen scale factor
  226.   ax=0:ay=0:az=0:' Angles in Degrees
  227.   px=200:py=100:' x,y Image Location
  228.   ef=0:' Erase Flag
  229.   f=57.2958:' Degrees to Radians factor
  230. RETURN
  231.   
  232.  
  233. DrawImage:
  234. ' Draw the Image
  235.     GOSUB Rotate
  236.     GOSUB DrawIt
  237. RETURN
  238.  
  239.  
  240.  
  241. Rotate:
  242.  ' First get trig values from angles
  243.  sx=SIN(ax/f):cx=COS(ax/f)
  244.  sy=SIN(ay/f):cy=COS(ay/f)
  245.  sz=SIN(az/f):cz=COS(az/f)
  246.  ' Then compute rotation values  
  247.  xRx=cy*cz
  248.  yRx=-cy*sz
  249.  zRx=-sy
  250.  xRy=cx*sz-sx*sy*cz
  251.  yRy=cx*cz+sx*sy*sz
  252.  zRy=-sx*cy
  253.  xRz=sx*sz+cx*sy*cz
  254.  yRz=sx*cz-cx*sy*sz
  255.  zRz=cx*cy
  256.  ' Now Rotate Image
  257.  np=0
  258.  Rotate1:
  259.   ' Get next point
  260.   c=it%(np,0):IF c=-1 THEN RETURN
  261.   x=it%(np,1):y=it%(np,2):z=it%(np,3)
  262.   ' Compute its new location
  263.   rim%(np,1)=x*xRx+y*yRx+z*zRx
  264.   rim%(np,2)=x*xRy+y*yRy+z*zRy
  265.   rim%(np,3)=x*xRz+y*yRz+z*zRz
  266.   np=np+1
  267.   GOTO Rotate1
  268.  
  269. DrawIt:
  270.  np=0:IF ef=-1 THEN CLS
  271.  DrawIt1:
  272.   ' Check for end of table
  273.   c=it%(np,0):IF c=-1 THEN RETURN
  274.   ' Keep from dividing by zero
  275.   IF (rim%(np,3)+di) = 0 THEN rim%(np,3)=rim%(np,3)+1
  276.   ' Compute screen x & y 
  277.   xw=px+(rim%(np,1)/(rim%(np,3)+di))*dw*sf
  278.   yw=py-(rim%(np,2)/(rim%(np,3)+di))*dw
  279.   ' Draw next line or move to next point
  280.   IF c=0 THEN GOTO JustMove
  281.   colr=c:IF ef=1 THEN colr=0
  282.   LINE (lx,ly)-(xw,yw),colr
  283.   JustMove: lx=xw:ly=yw
  284.   np=np+1
  285.   GOTO DrawIt1
  286.  
  287.      
  288. SetImage:
  289. ' Routine to insert an image into the table
  290.   n=0
  291.   itloop:
  292.     READ it%(n,0)
  293.     IF it%(n,0)=-1 THEN RETURN
  294.     READ it%(n,1),it%(n,2),it%(n,3)
  295.     n=n+1:GOTO itloop
  296.  
  297.  
  298. ' Greeting Image
  299. ' Image Data Format:c,x,y,z 
  300. '   (c=color, if =0 then move w/o drawing)
  301.   DATA 0,-50,30,0
  302.   DATA 1,-55,35,10
  303.   DATA 1,-45,0,0
  304.   DATA 1,-20,-60,-30
  305.   DATA 1,20,-60,-30
  306.   DATA 1,20,-60,-30
  307.   DATA 1,45,0,0
  308.   DATA 1,55,35,10
  309.   DATA 1,50,30,0
  310.   DATA 3,30,80,-30
  311.   DATA 3,-30,80,-30
  312.   DATA 3,-50,30,0
  313.   DATA 0,0,22,-30
  314.   DATA 1,0,-4,-36
  315.   DATA 0,-5,0,-30
  316.   DATA 1,0,-4,-36
  317.   DATA 1,5,0,-30
  318.   DATA 0,-20,30,-25
  319.   DATA 1,-35,25,-17
  320.   DATA 1,-20,20,-25
  321.   DATA 1,-5,25,-21
  322.   DATA 1,-20,30,-25
  323.   DATA 2,-20,20,-25
  324.   DATA 0,20,30,-25
  325.   DATA 1,35,25,-17
  326.   DATA 1,20,20,-25
  327.   DATA 1,5,25,-21
  328.   DATA 1,20,30,-25
  329.   DATA 2,20,20,-25
  330.   DATA 0,-20,-26,-22
  331.   DATA 3,0,-34,-30
  332.   DATA 3,20,-26,-22
  333.   DATA 0,-10,-30,-26
  334.   DATA 3,10,-30,-26
  335.   DATA -1
  336.   
  337.  
  338. ' Delta Wing Fighter Image
  339.   DATA 0,0,-20,100
  340.   DATA 1,0,20,-100
  341.   DATA 0,50,-20,-100
  342.   DATA 1,0,-20,100
  343.   DATA 1,-50,-20,-100
  344.   DATA 2,0,20,-100
  345.   DATA 2,50,-20,-100
  346.   DATA 2,-50,-20,-100
  347.   DATA 0,-75,0,-100
  348.   DATA 3,0,0,0
  349.   DATA 3,75,0,-100
  350.   DATA 3,-75,0,-100
  351.   DATA -1:' End of Image
  352.   
  353. ' Chaser Image
  354.   DATA 0,-25,0,-25
  355.   DATA 1,25,0,-25
  356.   DATA 1,25,0,25
  357.   DATA 1,-25,0,25
  358.   DATA 1,-25,0,-25
  359.   DATA 0,-25,25,25
  360.   DATA 3,-25,-25,25
  361.   DATA 3,-25,-25,-25
  362.   DATA 3,-25,25,-25
  363.   DATA 3,-25,25,25
  364.   DATA 0,25,25,25
  365.   DATA 3,25,-25,25
  366.   DATA 3,25,-25,-25
  367.   DATA 3,25,25,-25
  368.   DATA 3,25,25,25
  369.   DATA 0,0,0,-25
  370.   DATA 2,0,0,50
  371.   DATA 2,0,10,25
  372.   DATA 2,0,0,-25
  373.   DATA -1
  374.     
  375. ' XYZ axis Image
  376.   DATA 0,-100,0,0
  377.   DATA 1,100,0,0
  378.   DATA 1,80,-20,0
  379.   DATA 0,100,0,0
  380.   DATA 1,80,20,0
  381.   DATA 0,140,14,0
  382.   DATA 1,170,-16,0
  383.   DATA 0,140,-16,0
  384.   DATA 1,170,14,0
  385.   DATA 0,0,-100,0
  386.   DATA 2,0,100,0
  387.   DATA 2,20,80,0
  388.   DATA 0,0,100,0
  389.   DATA 2,-20,80,0
  390.   DATA 0,0,120,0
  391.   DATA 2,0,134,0
  392.   DATA 2,14,148,0
  393.   DATA 0,0,134,0
  394.   DATA 2,-14,148,0
  395.   DATA 0,0,0,100
  396.   DATA 3,0,0,-100
  397.   DATA 3,0,-20,-80
  398.   DATA 0,0,0,-100
  399.   DATA 3,0,20,-80
  400.   DATA 0,-14,14,-140
  401.   DATA 3,16,14,-140
  402.   DATA 3,-14,-16,-140
  403.   DATA 3,16,-16,-140
  404.   DATA -1
  405.   
  406.